package com.nf.health.app.homepage.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.Html;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nf.health.app.Constants;
import com.nf.health.app.R;
import com.nf.health.app.adapter.ResultAdapter;
import com.nf.health.app.core.DialogOnitem;
import com.nf.health.app.customview.BottomSslideDialog;
import com.nf.health.app.customview.CircleGradeProgress;
import com.nf.health.app.customview.CustomShareBoard;
import com.nf.health.app.customview.MyListView;
import com.nf.health.app.utils.AppManager;
import com.nf.health.app.utils.SharePlatform;
import com.nf.health.app.utils.ViewUtil;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.media.QQShareContent;
import com.umeng.socialize.media.QZoneShareContent;
import com.umeng.socialize.media.SinaShareContent;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.sso.SinaSsoHandler;
import com.umeng.socialize.sso.UMSsoHandler;
import com.umeng.socialize.weixin.media.CircleShareContent;
import com.umeng.socialize.weixin.media.WeiXinShareContent;

import java.util.List;

/** 
 * @ClassName: EndCheckActivity 
 * @Description:end 测评页面
 * @author Ian 
 * @date 2015-11-4 上午9:49:01 
 *  
 */
public class EndCheckActivity extends BaseForEvaluationActivity{


	private final UMSocialService mController = UMServiceFactory
			.getUMSocialService(Constants.DESCRIPTOR);
	String[] reportArr;//分享

	private MyListView listview_bg;

	private View backView;
	private TextView tv_endcheck_total,tv_endcheck_score,tv_endcheck_result;
	private ImageView btn_endcheck_back,titlebar_right_view;

	private CircleGradeProgress progress;

	private RelativeLayout tuxiang_frame;

	int pro_num;

	String title,httpUrl;

	Bitmap b1;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// TODO Auto-generated method stub
		setTitleBar(R.layout.titlebar_base);
		setContent(R.layout.activity_endcheck);
		TextView titleTV = (TextView) findViewById(R.id.titlebar_title_tv);
		titleTV.setText("检测结果");

		provider.requestAddScore("8","");


		backView = findViewById(R.id.titlebar_back_view);
		httpUrl = getIntent().getStringExtra("url");

		progress = (CircleGradeProgress) findViewById(R.id.progress);
		listview_bg = (MyListView) findViewById(R.id.listview_bg);
		tv_endcheck_total = (TextView) findViewById(R.id.tv_endcheck_totalId);
		tv_endcheck_score = (TextView) findViewById(R.id.tv_endcheck_scoreId);
		tv_endcheck_result = (TextView) findViewById(R.id.tv_endcheck_resultId);
		btn_endcheck_back = (ImageView) findViewById(R.id.btn_endcheck_backId);
		titlebar_right_view = (ImageView) findViewById(R.id.titlebar_right_view);//分享按钮
		tuxiang_frame = (RelativeLayout) findViewById(R.id.tuxiang_frame);//进度条

		ViewUtil.setImageResoure(this, R.id.titlebar_right_view, R.drawable.ic_title_more);

		initData();
		initListener();
	}


	private void setShareContent() {

		// 配置SSO
		mController.getConfig().setSsoHandler(new SinaSsoHandler());
		UMImage umImage;
		umImage = new UMImage(this,b1);
		String targetUrl = httpUrl;
		String content = "我的体质是"+getIntent().getStringExtra("result")+"质，快来测一测你属于哪种体质。";
		String title = "";

		// 微信分享必须设置targetURL，需要为http链接格式
		// 微信朋友圈只能显示title，并且过长会被微信截取部分内容
		WeiXinShareContent weixinContent = new WeiXinShareContent();
		weixinContent.setShareContent(content);
		weixinContent.setTitle(title);
		weixinContent.setTargetUrl(targetUrl);
		weixinContent.setShareMedia(umImage);
		mController.setShareMedia(weixinContent);

		// 设置朋友圈分享的内容
		CircleShareContent circleMedia = new CircleShareContent();
		circleMedia.setShareContent(content);
		circleMedia.setTitle(title);
		circleMedia.setShareMedia(umImage);
		circleMedia.setTargetUrl(targetUrl);
		mController.setShareMedia(circleMedia);

		QQShareContent qqShareContent = new QQShareContent();
		qqShareContent.setShareContent(content);
		qqShareContent.setTitle(title);
		qqShareContent.setShareMedia(umImage);
		qqShareContent.setTargetUrl(targetUrl);
		mController.setShareMedia(qqShareContent);
		// 设置QQ空间分享内容
		QZoneShareContent qzone = new QZoneShareContent();
		qzone.setTargetUrl(targetUrl);
		qzone.setShareContent(content);
		qzone.setTitle(title);
		qzone.setShareMedia(umImage);
		mController.setShareMedia(qzone);
		//新浪微博、腾讯微博及豆瓣的跳转链接只能设置在分享文字之中，以http形式传递即可，人人网可以单独设置跳转链接，方法为：
		SinaShareContent sinaContent = new SinaShareContent();
		sinaContent.setShareContent(title+httpUrl);
		//		sinaContent.setTitle(title);
		sinaContent.setShareImage(umImage);
		mController.setShareMedia(sinaContent);
	}


	private void configPlatforms() {
		// 添加新浪SSO授权
		mController.getConfig().setSsoHandler(new SinaSsoHandler());
		SharePlatform.addQQ_QZonePlatform(this);
		SharePlatform.addWXPlatform(this);
	}

	private void initData() {
		// TODO Auto-generated method stub

		@SuppressWarnings("unchecked")
		List listSers =(List)getIntent().getSerializableExtra("list");

		String aa =listSers+"";

		if(aa.contains("[]")){
			listview_bg.setVisibility(View.GONE);
			titlebar_right_view.setVisibility(View.GONE);
		}else{

			titlebar_right_view.setVisibility(View.VISIBLE);
			listview_bg.setVisibility(View.VISIBLE);
			ResultAdapter adapter = new ResultAdapter(EndCheckActivity.this,listSers);
			listview_bg.setAdapter(adapter);



			titlebar_right_view.setOnClickListener(new OnClickListener() {//健康自测分享

				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub

				}
			});


		}

		String check_result = getResources().getString(R.string.check_result1);
		String result = String.format(check_result, getIntent().getIntExtra("count",-1),getIntent().getIntExtra("score", -1));
		String health = getIntent().getStringExtra("result");
		tv_endcheck_total.setText(Html.fromHtml(result));
		tv_endcheck_result.setText(health);
		tv_endcheck_score.setText(getIntent().getIntExtra("score", -1)+"分");

		pro_num = getIntent().getIntExtra("score", -1);

		progress.setProgress(pro_num);//进度条赋值

	}

	private void initListener() {
		// TODO Auto-generated method stub
		backView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				AppManager.getAppManager().finishActivity();
				AppManager.getAppManager().finishActivity();
			}
		});
		btn_endcheck_back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		titlebar_right_view.setOnClickListener(new OnClickListener() {//分享按钮监听

			@Override
			public void onClick(View arg0) {
				//初始化  分享dialog


				tuxiang_frame.buildDrawingCache();
				tuxiang_frame.setDrawingCacheEnabled(true);
				b1 = tuxiang_frame.getDrawingCache(); 
//				showBottomDialog();



				CustomShareBoard shareBoard = new CustomShareBoard(EndCheckActivity.this);
				shareBoard.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
				//再设置模式，和Activity的一样，覆盖，调整大小。
				shareBoard.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
				shareBoard.showAtLocation(getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);
				shareBoard.showAtLocation(getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);


				// 配置需要分享的相关平台
				configPlatforms();
				// 设置分享的内容
				setShareContent();
			}
		});
	}
	@SuppressLint("NewApi")
	private void showBottomDialog() {
		reportArr = new String[] {"","分享"};
		new BottomSslideDialog(EndCheckActivity.this, true, null,reportArr, new DialogOnitem() {
			@Override
			public void onItemClickListener(int position) {
				switch (position) {
				case 0:

					break;
				default:
					break;
				}

			}
		}).create();
	}
	@Override 
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		/**使用SSO授权必须添加如下代码 */
		UMSsoHandler ssoHandler = mController.getConfig().getSsoHandler(requestCode) ;
		if(ssoHandler != null){
			ssoHandler.authorizeCallBack(requestCode, resultCode, data);
		}
	}

	@Override
	protected void onDestroy() {
		mController.getConfig().cleanListeners();
		super.onDestroy();
	}
}
